package org.spring.springboot.app.adv.advcombinfo.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.adv.advcombhard.domain.po.AdvCombHardPO;
import org.spring.springboot.app.adv.advcombinfo.domain.po.AdvCombInfoPO;
import org.spring.springboot.app.adv.advcombinfo.domain.vo.AdvCombInfoDetailVO;
import org.spring.springboot.app.adv.advcombinfo.domain.vo.AdvCombInfoListVO;
import org.spring.springboot.app.adv.advcombinfo.domain.vo.AdvCombInfoSaveInVO;
import org.spring.springboot.app.adv.advcombinfo.domain.vo.AdvCombInfoSearchVO;
import org.spring.springboot.app.adv.advcombinfo.service.IAdvCombInfoService;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.base.User;
import org.spring.springboot.app.sys.area.domain.po.SysAreaPO;
import org.spring.springboot.app.sys.area.service.ISysAreaService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(description="广告套餐操作接口",tags = ApiIndex.AdvCombInfoController)
@RequestMapping(value = "/api/advCombInfo")
@RestController
@AccessTokenVerify
public class AdvCombInfoController {

    @Autowired
    private IAdvCombInfoService advCombInfoService;
    @Autowired
    private ISysAreaService sysAreaService;


    @ApiOperation(value = "查询分页列表")
    @RequestMapping(value = "", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<PageInfo<List<AdvCombInfoListVO>>> findPage(
            @ApiIgnore User user,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize,
            @ApiParam(value = "名称", required = false) @RequestParam(defaultValue = "") String name,
            @ApiParam(value = "代理商ID", required = false) @RequestParam(defaultValue = "") String officeId
            ) {
        R result = new R();
        AdvCombInfoSearchVO searchVO = new AdvCombInfoSearchVO();
        if(StringUtils.isBlank(officeId)){
            officeId=user.getOfficeId();
        }
        searchVO.setOfficeId(officeId);
        searchVO.setName(name);
        List<AdvCombInfoPO> list = advCombInfoService.findPage(searchVO, pageNum, pageSize);
        PageInfo p = new PageInfo(list);
        p.setList(getVOS(p.getList()));
        result.setData(p);
        return result;
    }

    private List<AdvCombInfoListVO> getVOS(List<AdvCombInfoPO> _list) {
        List<AdvCombInfoListVO> list = new ArrayList<AdvCombInfoListVO>();
        if (_list != null && _list.size()>0) {
            for (AdvCombInfoPO po : _list) {
                AdvCombInfoListVO vo = new AdvCombInfoListVO();
                BeanUtils.copyProperties(po,vo);
                list.add(vo);
            }
        }
        return list;
    }



    @ApiOperation(value="查询所有列表")
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<List<AdvCombInfoListVO>> findAll(
            @ApiIgnore User user,
            @ApiParam(value = "查询类型 1我的 2别人的", required = false) @RequestParam(defaultValue = "1") int queryType,
            @ApiParam(value = "区域id", required = false) @RequestParam(defaultValue = "") String areaId,
            @ApiParam(value = "名称", required = false) @RequestParam(defaultValue = "") String name,
            @ApiParam(value = "代理商ID", required = false) @RequestParam(defaultValue = "") String officeId
    ) {
        R result = new R();
        AdvCombInfoSearchVO searchVO = new AdvCombInfoSearchVO();
        searchVO.setQueryType(queryType);
        if(StringUtils.isBlank(officeId)){
            officeId=user.getOfficeId();
        }
        searchVO.setOfficeId(officeId);
        searchVO.setName(name);
        if(StringUtils.isNotBlank(areaId)){
            SysAreaPO area = sysAreaService.find(areaId);
            if (area != null) {
                searchVO.setAreaId(area.getId());
                searchVO.setAreaParentIds(area.getParentIds()+area.getId()+",");
            }
        }
        List<AdvCombInfoPO> list = advCombInfoService.findAll(searchVO);
        result.setData(list);
        return result;
    }


    @ApiOperation(value = "查询单个信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<AdvCombInfoDetailVO> find(
            @ApiParam(value = "id", required = true) @PathVariable("id") String id
    ) {
        R result = new R();
        AdvCombInfoPO po = advCombInfoService.find(id);
        AdvCombInfoDetailVO vo = new AdvCombInfoDetailVO();
        BeanUtils.copyProperties(po,vo);
        //查询套餐关联的设备
        List<AdvCombHardPO> hardList = advCombInfoService.queryHardList(vo.getId());
        String hardIds = "";
        if (hardList != null) {
            for (AdvCombHardPO hard : hardList) {
                if(StringUtils.isNotBlank(hardIds)){
                    hardIds += ",";
                }
                hardIds += hard.getHardId();
            }
        }
        vo.setHardIds(hardIds);
        result.setData(vo);
        return result;
    }



    @ApiOperation(value="添加信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(@ApiIgnore User user, @ApiParam(value = "对象", required = true) @RequestBody AdvCombInfoSaveInVO vo, BindingResult bindingResult) {
        R result = new R();
        if (bindingResult.hasErrors()) {
            result.setCode(1);
            result.setMessage(bindingResult.getAllErrors().get(0).toString());
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        vo.setOfficeId(user.getOfficeId());
        return advCombInfoService.save(vo);
    }

    @ApiOperation(value="修改信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R update(@ApiIgnore User user,@ApiParam(value = "对象", required = true) @Valid  @RequestBody AdvCombInfoSaveInVO vo, BindingResult bindingResult) {
        R result = new R();
        if (bindingResult.hasErrors()) {
            result.setCode(1);
            result.setMessage(bindingResult.getAllErrors().get(0).toString());
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        return advCombInfoService.update(vo);
    }


    @ApiOperation(value="删除信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R delete(@ApiIgnore User user,@ApiParam(value = "id", required = true)  @PathVariable("id") String id) {
        R result = new R();
        if (StringUtils.isBlank(id)) {
            result.setType(Type.ParamValidateFail);
            result.setMessage("id不能为空");
            return result;
        }
        AdvCombInfoPO po = new AdvCombInfoPO();
        po.setId(id);
        int code= advCombInfoService.delete(po);
        if (code == 0) {
            result.setCode(1);
            result.setMessage("修改失败");
        }
        return result;
    }

}
